﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WZ.Entity;
using WZ.WebSpider;

namespace WZ.ST.Test
{
    class Program
    {
        static void Main(string[] args)
        {

            //insertAnnouncementContent();
            //getCompanyMarketData();
            //insertAnnouncement();
            insertCompanyClass();
            Console.Read();
        }


        static void insertCompanyMarketData()
        {
            List<WZ.Entity.Commpany> companyList = new List<WZ.Entity.Commpany>();
            WZ.Stock.Data.CommpanyDAL dal = new WZ.Stock.Data.CommpanyDAL();
            WZ.Stock.Data.StockMarketDAL dalS = new WZ.Stock.Data.StockMarketDAL();
            companyList = dal.GetAllCompany();
            foreach (var item in companyList)
            {
                WZ.WebSpider.StockMarketSpider spider = new WZ.WebSpider.StockMarketSpider(item.Code);
                List<WZ.Entity.StockMarket> market = spider.GetWebData("");

                Console.WriteLine(string.Format("公司名：{0}，代码：{1}，数据库ID：{2}", market[0].SimplyName, market[0].Code, dalS.InsertData(market[0])));
                Thread.Sleep(500);
            }
        }
        static void insertAnnouncement()
        {
            int pageNum = 1;
            bool isExists = false;
            while (pageNum < 200)
            {
                //string urlParams = string.Format("stock=&searchkey=&plate=&category=&trade=&column=sse&columnTitle=%E6%B2%AA%E5%B8%82%E5%85%AC%E5%91%8A&pageNum={0}&pageSize=30&tabName=latest&sortName=&sortType=&limit=&showTitle=&seDate=%E8%AF%B7%E9%80%89%E6%8B%A9%E6%97%A5%E6%9C%9F", pageNum);
                string urlParams = string.Format("stock=&searchkey=&plate=&category=&trade=&column=szse&columnTitle=%E5%8E%86%E5%8F%B2%E5%85%AC%E5%91%8A%E6%9F%A5%E8%AF%A2&pageNum={0}&pageSize=30&tabName=fulltext&sortName=&sortType=&limit=&showTitle=&seDate={1}", pageNum, "");
                WZ.WebSpider.AnnouncementSpider spider = new WZ.WebSpider.AnnouncementSpider();
                List<WZ.Entity.Announcement> market = spider.GetWebData(urlParams);
                foreach (var item in market)
                {
                    try
                    {
                        isExists = spider.Save(item);
                        Console.WriteLine(string.Format("公司名：{0}，代码：{1}，数据库ID：{2}", item.secName, item.secCode, item.Id));
                        // Thread.Sleep(200);
                    }
                    catch
                    {
                        continue;
                    }


                }
                pageNum++;
            }

        }
        static void insertAnnouncementContent()
        {
            WZ.Stock.Data.AnnouncementDAL dal = new Stock.Data.AnnouncementDAL();
            List<Announcement> annList = new List<Announcement>();
            annList = dal.SelectTo().ToList();
            foreach (var item in annList)
            {
                string content = string.Empty;
                if (item.adjunctType.ToLower().Contains("pdf"))
                {
                    content = WZ.UtilityLibray.PDF.ReaderContent.Read(item.adjunctUrl);
                }
                WZ.Stock.Data.AnnouncementContentDAL contentDal = new Stock.Data.AnnouncementContentDAL();
                contentDal.InsertData(new AnnouncementContent
                {
                    announcementId = item.announcementId,
                    Content = content,
                    IsParse = !string.IsNullOrEmpty(content),
                    PdfUrl = item.adjunctUrl
                });
                Console.WriteLine(string.Format("公司名：{0}，代码：{1}，数据库ID：{2}，读取结果：{3}", item.secName, item.secCode, item.Id, !string.IsNullOrEmpty(content)));

            }
        }
        static void getCompanyMarketData()
        {
            CompanyMarketDataSpider cmds = new CompanyMarketDataSpider("600369");
            Console.WriteLine(Common.JsonHelper.SerializeObject(cmds.GetWebData("")));
        }
        static void insertDicCompanyClass()
        {
            string[] types = { "class", "industry", "area" };
            foreach (var t in types)
            {
                WebSpider.dic_CompanyClassSpider s = new dic_CompanyClassSpider();
                List<dic_CompanyClass> c = s.GetWebData(t);
                foreach (var item in c)
                {
                    s.Save(item);
                    Console.WriteLine(string.Format("类别：{0}，标题：{1}，代码：{2}，数据库ID：{3}", item.TypeName, item.Title, item.Code, item.Id));
                }
            }
        }
        static void insertCompanyClass()
        {
            WZ.Stock.Data.dic_CompanyClassDAL dal = new Stock.Data.dic_CompanyClassDAL();

            List<dic_CompanyClass> c = dal.SelectTo().ToList();
            foreach (var item in c)
            {
                dynamic result = WebSpider.JSONSpider.Commpany_ClassSpider.GetData(item.Code);
                foreach (var d in result)
                {
                    dal.insertCommpanyClass(d.code.Value, item.TypeName.Trim(), item.Id);
                    Console.WriteLine(string.Format("类别：{0}，标题：{1}，公司代码：{2}", item.Title, item.TypeName, d.code));
                }
            }
        }
    }
}
